package com.shengruan.admin.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.jdbc.OrdersJdbc;
import com.shengruan.admin.jdbc.ProductJdbc;
import com.shengruan.admin.jdbc.ShippingJdbc;
import com.shengruan.admin.vo.Orders;
import com.shengruan.admin.vo.Product;
import com.shengruan.admin.vo.Shipping;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.framework.pager.Pager;

public class OrdersAction extends BaseAction {

    Logger logger = LoggerFactory.getLogger(getClass());

    private OrdersJdbc ordersJdbc = (OrdersJdbc) this.getJdbc(OrdersJdbc.class);
    private ProductJdbc productJdbc = (ProductJdbc) this.getJdbc(ProductJdbc.class);
    private ShippingJdbc shippingJdbc = (ShippingJdbc) this.getJdbc(ShippingJdbc.class);

    /**
     * list
     * @throws Exception
     */
    public void list() throws Exception {
        String orderNumber = this.input("orderNumber");
        String status = this.input("status");
        String ordersReport = this.input("ordersReport");

        int currentPage = 1;
		try {
			currentPage = Integer.parseInt(this.input("currentPage"));
		} catch(Exception ex) { }

        Pager pager = new Pager(currentPage,ordersJdbc.getRows(orderNumber, status, ordersReport),30);
        List<Orders> ordersList = ordersJdbc.findWithPage(currentPage, 30, orderNumber, status, ordersReport);

        this.setRequest("orderNumber", orderNumber);
        this.setRequest("status", status);
        this.setRequest("ordersReport", ordersReport);
		this.setRequest("pager", pager);
		this.setRequest("ordersList", ordersList);
        this.setRequest("ORDERS_REPORT_STATUS_MAP", Constants.ORDERS_REPORT_STATUS_MAP);



        this.forward("/admin/order/order-list.jsp");
    }


    public void getById() throws Exception {
        String id = this.input("id");
        Orders orders = this.ordersJdbc.getById(Integer.valueOf(id));
        List<Product> orderProductList = this.productJdbc.getByOrderId(orders.getId());
        Shipping shipping = this.shippingJdbc.getById(orders.getShippingId());

        this.setRequest("orders", orders);
        this.setRequest("shipping", shipping);
        this.setRequest("orderProductList", orderProductList);
        this.setRequest("ORDERS_STATUS_MAP", Constants.ORDERS_STATUS_MAP);
        this.forward("/admin/order/order-details.jsp");
    }

    public void edit() throws Exception {
        String id = this.input("id");
        String ordersStatus = this.input("ordersStatus");

        Orders orders = this.ordersJdbc.getById(Integer.valueOf(id));
        orders.setStatus(Integer.valueOf(ordersStatus));
        this.ordersJdbc.update(orders);
        this.redirect("/admin/order/order-list.ac");
    }


     /**
     * 取消订单
     *
     * @throws Exception
     */
    public void delete() throws Exception {

        String id = this.input("id");
        this.ordersJdbc.deleteById(Integer.valueOf(id));

        this.redirect("/admin/order/order-list.ac");
    }
}
